<?php
namespace Api\Controller;
use Common\Model\FileModel;
use Common\Model\FileLinkModel;
use Common\Model\LogInfoModel;

/**
 * 文件相关操作
 * 
 * @author xiaoyutab<xiaoyutab@qq.com>
 * @adtime 2018-03-15 17:46:27
 * @version v1.0.0
 * @copyright (c) 2017, xiaoyutab
 */
class FileController extends ApiController {
    /**
     * 上传图片信息
     */
    public function uploadAction(){
        $get = I('get.');
        if($get['CKEditor']){
            // 标明如果是CKEditor上传的图片
            $file = new FileModel();
            $file = $file->UploadOne('upload','ck_upload');
            echo '<script type="text/javascript">';
            echo 'window.parent.CKEDITOR.tools.callFunction('.$get['CKEditorFuncNum'].',"'.C('Upload.IMAGE_SAVE_PATH', null, './Public/Upload/').$file['upload']['savepath'].$file['upload']['savename'].'","")';
            echo '</script>';
        }
    }
    
    /**
     * ajax传输图片
     */
    public function upload_ajaxAction(){
        // 如果是post传输的数据【带table_name、table_id】
//        dump(I('post.'));exit;
        if(I('post.')){
            $_file = new FileModel();
            $_file_link = new FileLinkModel();
            $_log_link = new LogInfoModel();
            $file = $_file->Upload('ajaxUpload','ajax_upload');
            $return_data = [];
            $return_data['file_path'] = $file['ajaxUpload']['file_path'];
            $return_data['file_id'] = $file['ajaxUpload']['file_id'];
            $return_data['link_id'] = $_file_link->create_info([
                'file_id' => $file['ajaxUpload']['file_id'],
                'target_id' => I('post.table_id','0','intval'),
                'target_name' => $_log_link->get_id(I('post.table_name','table','trim'))
            ]);
            echo json_encode($return_data);exit;
        }
    }
}
